package com.niknik.woody.core;

import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.inject.Inject;

import java.text.MessageFormat;

public class ServerToClientTransmitter {

  static final String CHANNEL_NAME_PATTERN = "{0}_{1}";

  @Inject private ChannelService channelService;

  public void publish(String roomId, String playerId, String data) {
    channelService.sendMessage(new ChannelMessage(buildChannelName(roomId, playerId), data));
  }

  public String createChannel(final String roomId, final String playerId) {
    return channelService.createChannel(buildChannelName(roomId, playerId));
  }

  private String buildChannelName(final String roomId, final String playerId) {
    return MessageFormat.format(CHANNEL_NAME_PATTERN, roomId, playerId);
  }
}
